﻿using System.Threading.Tasks;
using System.Windows;

namespace Red.Controls.Shell
{
    /// <summary>
    ///     Standard message box
    /// </summary>
    public partial class RedMessageBox : RedDialog
    {
        #region Public Types

        public enum Mode
        {
            None,
            Info,
            Warning
        }

        #endregion Public Types

        #region Data

        private static string _dafaultTitle = "Message";
        
        #endregion Data

        #region Properties

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register( "Message", typeof( string ), typeof( RedMessageBox ) );

        /// <summary>
        ///     Content of the message
        /// </summary>
        public string Message
        {
            get { return (string)GetValue( MessageProperty ); }
            set { SetValue( MessageProperty, value ); }
        }

        public object IconCanvas { get; private set; }
        public bool ShowIcon { get; private set; }

        #endregion Properties

        #region C-tor

        private RedMessageBox( FrameworkElement elem, Mode mode = Mode.None )
            : base( elem )
        {
            SetMode( mode );
            InitializeComponent();
        }

        #endregion C-tor

        #region Public methods
        
        /// <summary>
        ///     Shows a message box owned by top-level window in the elem hierarchy
        /// </summary>
        public static bool Show( FrameworkElement elem, RedDialogType type, string message, string title = null, Mode mode = Mode.None )
        {
            var msg = new RedMessageBox( elem, mode ) { Type = type, Message = message, Title = title ?? _dafaultTitle };
            return msg.Show();
        }

        /// <summary>
        ///     Shows a message box owned by top-level window in the elem hierarchy. 
        ///     Async version is like showing the dialog non-modally, with task finished when user takes an action.
        /// </summary>
        public static Task<bool> ShowAsync( FrameworkElement elem, RedDialogType type, string message, string title = null, Mode mode = Mode.None )
        {
            var msg = new RedMessageBox( elem, mode ) { Type = type, Message = message, Title = title ?? _dafaultTitle };
            return msg.ShowAsync();
        }

        #endregion Public methods

        #region Private Methods

        private void SetMode( Mode mode )
        {
            switch ( mode )
            {
                case Mode.None:
                    ShowIcon = false;
                    break;

                case Mode.Info:
                    IconCanvas = TryFindResource( "appbar_information_circle" );
                    ShowIcon = true;
                    break;

                case Mode.Warning:
                    IconCanvas = TryFindResource( "appbar_warning_circle" );
                    ShowIcon = true;
                    break;
            }
        }

        #endregion Private Methods
    }
}
